package com.jinzhi.jzweb.controller;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzapi.examination.pojo.ExaminationsVo;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.pojo.*;
import com.jinzhi.jzweb.pojo.MarkStatisticsExcel;
import com.jinzhi.jzweb.pojo.ModuleConfig.JzJinzhuanDispositionTopic;
import com.jinzhi.jzweb.pojo.ModuleConfig.StatisticsVO;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.jzweb.service.moduleConfig.IJzJinzhuanDispositionTopicService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.jzweb.utils.ZipFileUtils;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 打分表
 * </pre>
 * <small> 2023-06-19 08:45:15 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/mark")
@Api(value = "打分", tags = "打分")
public class MarkController extends BaseController {
	@Autowired
	private MarkService markService;

	@Autowired
	private ExaminationBatchService examinationBatchService;

	@Autowired
	private ExaminationResultsService examinationResultsService;

	@Autowired
	private ExaminationBatchComSService examinationBatchComSService;

	@Autowired
	private ExaminationBatchComService examinationBatchComService;


	@Autowired
	private IJzJinzhuanDispositionTopicService dispositionTopicService;

	@Autowired
	private StuSignUpComService stuSignUpService;

	@Autowired
	private ExaminationAnswerSheetComService examinationAnswerSheetComService;




	/**
	 * 查询实操考试批次（列表数据）
	 * @return
	 */
	@ResponseBody
	@PostMapping("/selEbatchId")
	public Result<List<ExaminationBatchDO>> selEbatchId(@RequestBody ExaminationBatchDO  examinationBatchDO) {//Long competitionType
		EntityWrapper wrapper =new EntityWrapper();
		if (Objects.nonNull(examinationBatchDO.getCompetitionType()) && !examinationBatchDO.getCompetitionType().equals(null) && !examinationBatchDO.getCompetitionType().equals("null") && !examinationBatchDO.getCompetitionType().equals("")){
			wrapper.addFilter("{0} like CONCAT('%',competition_type,'%')",examinationBatchDO.getCompetitionType());
		}
//		wrapper.eq("competition_type", 1);
		wrapper.eq("type",4);
		wrapper.orderBy("id",true);
		if (!StringUtils.isBlank(this.getUser().getTrack()) ){
				if(this.getUser().getTrack().equals("B") ||   this.getUser().getTrack().equals("C")){
					//等于A就理论打分的裁判
					wrapper.like("referee_id",this.getUserId().toString());
				}
		}
		if (!StringUtils.isBlank(this.getUser().getTrack()) && this.getUser().getTrack().equals("E")){
			//竞赛一级管理员
			wrapper.eq("create_id",this.getUserId());//创建人id
		}else if (!StringUtils.isBlank(this.getUser().getTrack()) && this.getUser().getTrack().equals("D")){
			//工作人员
			wrapper.like("encipher",this.getUserId().toString());//创建人id（工作人员id）
		}
		List<ExaminationBatchDO> examinationBatchDOList = examinationBatchService.selectList(wrapper);
		for (int i = 0;i < examinationBatchDOList.size();i++){
			EntityWrapper ebatchWrapper =new EntityWrapper();
			ebatchWrapper.eq("pid",examinationBatchDOList.get(i).getId());
			ebatchWrapper.eq("cbatch_id",examinationBatchDOList.get(i).getCbatchId());
			ebatchWrapper.orderBy("id",true);
			List<ExaminationBatchComSDO> ebatchlist = examinationBatchComSService.selectList(ebatchWrapper);
			examinationBatchDOList.get(i).setEbatchlist(ebatchlist);
		}
		return Result.ok(examinationBatchDOList);
	}


	/**
	 * 查询实操考试批次（列表数据），选手管理实操页面使用
	 * @return
	 */
	@ResponseBody
	@PostMapping("/selEbatchIds")
	public Result<List<ExaminationBatchDO>> selEbatchIds(@RequestBody ExaminationBatchDO  examinationBatchDO) {//Long competitionType
		EntityWrapper wrapper =new EntityWrapper();
		if (Objects.nonNull(examinationBatchDO.getCompetitionType()) && !examinationBatchDO.getCompetitionType().equals(null) && !examinationBatchDO.getCompetitionType().equals("null") && !examinationBatchDO.getCompetitionType().equals("")){
			wrapper.addFilter("{0} like CONCAT('%',competition_type,'%')",examinationBatchDO.getCompetitionType());
		}
//		wrapper.eq("competition_type", 1);
		wrapper.eq("type",3);
		wrapper.orderBy("id",true);
		if (!StringUtils.isBlank(this.getUser().getTrack()) ){
			if(this.getUser().getTrack().equals("B") ||   this.getUser().getTrack().equals("C")){
				//等于A就理论打分的裁判
				wrapper.like("referee_id",this.getUserId().toString());
			}
		}
		if (!StringUtils.isBlank(this.getUser().getTrack()) && this.getUser().getTrack().equals("E")){
			//竞赛一级管理员
			wrapper.eq("create_id",this.getUserId());//创建人id
		}else if (!StringUtils.isBlank(this.getUser().getTrack()) && this.getUser().getTrack().equals("D")){
			//工作人员
			wrapper.like("encipher",this.getUserId().toString());//创建人id（工作人员id）
		}
		List<ExaminationBatchDO> examinationBatchDOList = examinationBatchService.selectList(wrapper);
		for (int i = 0;i < examinationBatchDOList.size();i++){
			EntityWrapper ebatchWrapper =new EntityWrapper();
			ebatchWrapper.eq("pid",examinationBatchDOList.get(i).getId());
			ebatchWrapper.eq("cbatch_id",examinationBatchDOList.get(i).getCbatchId());
			ebatchWrapper.orderBy("id",true);
			List<ExaminationBatchComSDO> ebatchlist = examinationBatchComSService.selectList(ebatchWrapper);
			examinationBatchDOList.get(i).setEbatchlist(ebatchlist);
		}
		return Result.ok(examinationBatchDOList);
	}


	/**
	 * 查询理论考试批次（列表是数据）
	 * @return
	 */
	@ResponseBody
	@PostMapping("/selEbatchIdLiLuen")
	public Result<List<ExaminationBatchDO>> selEbatchIdLiLuen(@RequestBody ExaminationBatchDO  examinationBatchDO) {//Long competitionType
		EntityWrapper wrapper =new EntityWrapper();
		if (Objects.nonNull(examinationBatchDO.getCompetitionType()) && !examinationBatchDO.getCompetitionType().equals(null) && !examinationBatchDO.getCompetitionType().equals("null") && !examinationBatchDO.getCompetitionType().equals("")){
			wrapper.eq("competition_type", examinationBatchDO.getCompetitionType());
		}
		wrapper.eq("type",3); //查询理论批次
		if (!StringUtils.isBlank(this.getUser().getTrack()) && this.getUser().getTrack().equals("A")){
				//等于A就理论打分的裁判
			wrapper.like("refereeA",this.getUserId().toString());
		}

		if (!StringUtils.isBlank(this.getUser().getTrack()) && this.getUser().getTrack().equals("E")){
				//竞赛一级管理员
			wrapper.eq("create_id",this.getUserId());//创建人id
		}else if (!StringUtils.isBlank(this.getUser().getTrack()) && this.getUser().getTrack().equals("D")){
			//工作人员
			wrapper.like("encipher",this.getUserId().toString());//创建人id（工作人员id）
		}

		wrapper.orderBy("id",true);
		List<ExaminationBatchDO> examinationBatchDOList = examinationBatchService.selectList(wrapper);
		return Result.ok(examinationBatchDOList);
	}



//	/**
//	 * 分数统计导出
//	 * competition ABC赛场
//	 * ebatchId 考试批次
//	 * cbatchId 认证批次
//	 * @return
//	 */
//	@ResponseBody
//	@GetMapping("/scoreStatisticsexcel")
//	public void scoreStatisticsexcel(String competition, String ebatchId, String cbatchId, HttpServletResponse response){
//		Map map =new HashMap();
//		map.put("competition",competition); //根据赛场查询 abc必填
//		if (StringUtil.notEmpty(ebatchId)) { //如果考试批次不为空
//			map.put("ebatchId", ebatchId);
//		}
//		// 查询列表数据
//		List<MarkDO> list = markService.selectListMarkDO(map);
//		List<MarkExcel> markExcels =new ArrayList<>();
//		for (int i=0;i<list.size();i++){
//			ExaminationBatchComSDO examinationBatchComSDO = examinationBatchComSService.selectById(list.get(i).getRaceId());
//			list.get(i).setRaceName(examinationBatchComSDO.getName());
//			if (list.get(i).getRealityUm2() == null){
//				list.get(i).setRealityUm2("未参加考试");
//			}
//			MarkExcel markExcel =new MarkExcel();
//			markExcel.setNum2(list.get(i).getRaceName() + "-" + list.get(i).getRealityUm2()); //理论二次加密
//			markExcel.setUnitName(list.get(i).getOrgName()); //单位名称
//			markExcel.setName(list.get(i).getSignName()); //考生姓名
//			markExcel.setIdNumber(list.get(i).getIdentity()); //身份证
//
//			Map selRefereeIdMap =new HashMap();
//			selRefereeIdMap.put("ebatchId",list.get(i).getEbatchId()); //考试批次
//			selRefereeIdMap.put("competition",list.get(i).getCompetition()); //abc赛场
//			selRefereeIdMap.put("stuId",list.get(i).getSignId());//考生id
//
//			List<String> refereeId = markService.selectRefereeId(selRefereeIdMap); //查询出打分的三个裁判id
//			String scoreSun = markService.selTotalScore(selRefereeIdMap); //查询出三个裁判十五次打分的总数
//			Double totalScore = Double.valueOf(scoreSun);
//			totalScore =  (totalScore/3) * 0.7; //题目分数总和 / 裁判数量 * 0.7
//			totalScore = new BigDecimal(totalScore).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();//保留小数点后两位
//
//
//			List<String> scoreABC = markService.selScoreABC(selRefereeIdMap); //查询出分数
////			if (scoreABC == null){
////				markExcel.setScores1("0.0");
////				markExcel.setScores2("0.0");
////				markExcel.setScores3("0.0");
////			}
////			if (scoreABC.size() == 3){
////				markExcel.setScores1(scoreABC.get(0));
////				markExcel.setScores2(scoreABC.get(1));
////				markExcel.setScores3(scoreABC.get(2));
////			}
////			if (scoreABC.size() == 2){
////				markExcel.setScores1(scoreABC.get(0));
////				markExcel.setScores2(scoreABC.get(1));
////				markExcel.setScores3("0.0");
////			}
////			if (scoreABC.size() == 1){
////				markExcel.setScores1(scoreABC.get(0));
////				markExcel.setScores2("0.0");
////				markExcel.setScores3("0.0");
////			}
//			markExcel.setScore(Double.valueOf(totalScore));
//			markExcels.add(markExcel);
//		}
//		//导出操作
//		EasyPoiUtil.exportExcel(markExcels, competition +"赛场成绩导出", competition + "赛场成绩导出", MarkExcel.class, competition + "赛场成绩导出", response);
//	}



	/** 赛场统计数据导出操作 */
	@ResponseBody
	@GetMapping("/scorePracticalStatisticsexcel")
	public Result  scorePracticalStatisticsexcel(String cbatchId,String ebatchId,String competition,String competitionName,String competitionId,HttpServletResponse response)  {
		ArrayList<Long>	ids	=	new ArrayList<>();
		ArrayList<Long>	stuIds	=	new ArrayList<>();
		//导出的数据
		List<ScoreStatisticsexcel> excel = new ArrayList<>();
		List<Map<Object,Object>> excelList = new ArrayList<>();
		List<ScoreOneStatisticsexcel> excelOne = new ArrayList<>();
		List<ScoreTwoStatisticsexcel> excelTwo = new ArrayList<>();
		List<ScoreThreeStatisticsexcel> excelThree = new ArrayList<>();
		List<ScoreFourStatisticsexcel> excelFour = new ArrayList<>();
		List<ScoreFiveStatisticsexcel> excelFive = new ArrayList<>();
		//查询出实操赛场学员信息
		//查询模板任务
		//如果批次下面没有赛场
		ExaminationBatchComSDO examinationBatchComSDO = examinationBatchComSService.selectById(competitionId);
		if (examinationBatchComSDO == null){
//			Page<StuSignUpComDO> stuList = new Page<>();
//			statisticsVO.setStuSignUpComDO(stuList);
			EasyPoiUtil.exportExcel(excel, competitionName+"赛场成绩导出",  competitionName+"赛场成绩导出", ScoreStatisticsexcel.class, competitionName+"赛场成绩导出", response);
			return Result.fail("没有可导出的数据");
		}

		EntityWrapper<JzJinzhuanDispositionTopic> wrapperTopic = new EntityWrapper<>();
		wrapperTopic.eq("type",competition);//赛场（B）			//模板id
		wrapperTopic.eq("module_id",examinationBatchComSDO.getModileId());
		List<JzJinzhuanDispositionTopic> topicList = dispositionTopicService.selectList(wrapperTopic);//B赛场所有模板任务
		if (topicList.size() <= 0){
			EasyPoiUtil.exportExcel(excel, competitionName+"赛场成绩导出",  competitionName+"赛场成绩导出", ScoreStatisticsexcel.class, competitionName+"赛场成绩导出", response);
			return Result.fail("没有可导出的数据");
		}
		for (int i = 0;i<topicList.size();i++){
			if (topicList.get(i).getTopicName().contains(competitionName)){//如果相等就是安全防护任务
				//模板id
				ids.add(topicList.get(i).getId());
			}
		}
		StuSignUpComDO stuSignUpComDO = new StuSignUpComDO();
		stuSignUpComDO.setEbatchId(Long.valueOf(ebatchId));
		stuSignUpComDO.setCbatchId(Long.valueOf(cbatchId));
		stuSignUpComDO.setCompetition(competition);
		stuSignUpComDO.setJinzhuanTopicId(String.valueOf(ids.get(0)));
		stuSignUpComDO.setCompetitionId(competitionId);//赛场id
		stuSignUpComDO.setRaceId(examinationBatchComSDO.getId());
		List<StuSignUpComDO> stuList = stuSignUpService.ListUstASCExcel(stuSignUpComDO);
		if (stuList.size() <= 0){
			EasyPoiUtil.exportExcel(excel, competitionName+"赛场成绩导出",  competitionName+"赛场成绩导出", ScoreStatisticsexcel.class, competitionName+"赛场成绩导出", response);
			return Result.ok("没有可导出的数据");
		}
		for (int i = 0 ;i < stuList.size(); i++){
			//考生id
			stuIds.add(stuList.get(i).getId());
		}
		//查询出打分信息
		Map map =new HashMap();
		map.put("competition",competition);
		map.put("signId",stuIds);
		map.put("jinzhuanTopicId",ids);
		map.put("cbatchId",Long.valueOf(cbatchId));
		map.put("ebatchId",Long.valueOf(ebatchId));
		map.put("race_id",examinationBatchComSDO.getId());
		//查询出裁判数量
		List<MarkVO> mrkRefereeVOS = markService.selectRefereeBCSize(map);
		//裁判打分数据
		List<MarkVO> markVOS = markService.seleScoreTopicContent(map);
		for (int i = 0;i<stuList.size();i++){
			ArrayList<MarkVO>	MarkVOlist	=	new ArrayList<>();
			Map<Object, Object> ExcelMap = new HashMap<>();
//			ScoreStatisticsexcel scoreStatisticsexcel = new ScoreStatisticsexcel();
			//进行学员匹配
			ScoreOneStatisticsexcel scoreOneStatisticsexcel = new ScoreOneStatisticsexcel();
			ScoreTwoStatisticsexcel scoreTwoStatisticsexcel = new ScoreTwoStatisticsexcel();
			ScoreThreeStatisticsexcel scoreThreeStatisticsexcel = new ScoreThreeStatisticsexcel();
			ScoreFourStatisticsexcel scoreFourStatisticsexcel = new ScoreFourStatisticsexcel();
			ScoreFiveStatisticsexcel scoreFiveStatisticsexcel = new ScoreFiveStatisticsexcel();
			int s = 0;
			for (int k = 0;k<markVOS.size();k++){//这里长度多少就有几个裁判
				if (stuList.get(i).getId().equals(markVOS.get(k).getSignId())) {//用户表学员表与打分表考试id匹配
					//计算裁判打的这个任务的总分
					String[] refereeScores = markVOS.get(k).getRefereeScore().split("/");//分割出来
					BigDecimal refereeScore = new BigDecimal(0);
					for (String score : refereeScores) {
						refereeScore = new BigDecimal(score).add(refereeScore);//得到所有裁判打的总分
					}
					markVOS.get(k).setRefereeScore(String.valueOf(refereeScore));//将裁判打的这个任务的总分存入这个对象中
					//计算出这个学员这个任务中所获得的总分（每个裁判总分除以裁判数）//这里k等于几就是有几个裁判
					//如果相同那么将这条数据添加到用户集合对象中去
					MarkVOlist.add(markVOS.get(k));
					s = s + 1;
					ExcelMap.put("裁判"+ s ,markVOS.get(k).getRefereeScore());
				}
			}
			//将两个集合合并
			List<MarkVO> resultList = Stream.concat(MarkVOlist.stream(), mrkRefereeVOS.stream())
					.collect(Collectors.toList());
			//去掉重复的裁判数据
			List<MarkVO> resultListDuplicate = resultList.stream().filter(distinctByKey(b->b.getRefereeId())).collect(Collectors.toList());
			stuList.get(i).setMarkVO(resultListDuplicate);
			stuList.get(i).getMarkVO().size();//有多少条数就有多少个裁判
			Double TotalScore = 0.0;
			for (int j = 0; j< stuList.get(i).getMarkVO().size();j++){//这个学员的所有裁判打的安全防护分
				if (!stuList.get(i).getMarkVO().get(j).getRefereeScore().equals("-")){
					TotalScore = Double.valueOf(stuList.get(i).getMarkVO().get(j).getRefereeScore())  +TotalScore;//得到所有裁判打的总分
				}
			}

			//最终学员这个任务所获得的总分
			stuList.get(i).setTotalScore(new BigDecimal(TotalScore /stuList.get(i).getMarkVO().size()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());//最终得分为每个裁判打的总分除以裁判人数
			ExcelMap.put("赛场得分",stuList.get(i).getTotalScore());
			//设置五个导出类
			//循环
			//一个裁判
			if (stuList.get(i).getMarkVO().size() == 1){
				for (int p = 0;p < stuList.get(i).getMarkVO().size();p++){
					if (p == 0){
						scoreOneStatisticsexcel.setRefeereOneScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					}
					//将考生信息存入导出类中
					ExcelMap.put("一次加密序号",stuList.get(i).getComNums());
					scoreOneStatisticsexcel.setEncryptionUmber(stuList.get(i).getRealityNm1());
					ExcelMap.put("二次加密序号",stuList.get(i).getComNums());
					scoreOneStatisticsexcel.setEncryptionUmber2(stuList.get(i).getRealityNm2());
					ExcelMap.put("参赛单位",stuList.get(i).getUnitName());
					scoreOneStatisticsexcel.setUnitName(stuList.get(i).getUnitName());
					ExcelMap.put("姓名",stuList.get(i).getName());
					scoreOneStatisticsexcel.setName(stuList.get(i).getName());
					ExcelMap.put("身份证号码",stuList.get(i).getIdNumber());
					scoreOneStatisticsexcel.setIdNumber(stuList.get(i).getIdNumber());
					scoreOneStatisticsexcel.setTotalScore(stuList.get(i).getTotalScore());
				}
					String[] arr = stuList.get(i).getRefereeremark().split("/");
					Set<String> set = new HashSet<String>(Arrays.asList(arr));
					String result = String.join("/", set);
					scoreOneStatisticsexcel.setRefereeRemark(result);
			}
				//两个裁判
			   if (stuList.get(i).getMarkVO().size() == 2){
					for (int p = 0;p < stuList.get(i).getMarkVO().size();p++){
						if (p == 0){
							scoreTwoStatisticsexcel.setRefeereOneScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
						}else if (p == 1){
							scoreTwoStatisticsexcel.setRefeereTwoScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
						}
						//将考生信息存入导出类中
						ExcelMap.put("一次加密序号",stuList.get(i).getComNums());
						scoreTwoStatisticsexcel.setEncryptionUmber(stuList.get(i).getRealityNm1());
						ExcelMap.put("二次加密序号",stuList.get(i).getComNums());
						scoreTwoStatisticsexcel.setEncryptionUmber2(stuList.get(i).getRealityNm2());
						ExcelMap.put("参赛单位",stuList.get(i).getUnitName());
						scoreTwoStatisticsexcel.setUnitName(stuList.get(i).getUnitName());
						ExcelMap.put("姓名",stuList.get(i).getName());
						scoreTwoStatisticsexcel.setName(stuList.get(i).getName());
						ExcelMap.put("身份证号码",stuList.get(i).getIdNumber());
						scoreTwoStatisticsexcel.setIdNumber(stuList.get(i).getIdNumber());
						scoreTwoStatisticsexcel.setTotalScore(stuList.get(i).getTotalScore());
					}
					   String[] arr = stuList.get(i).getRefereeremark().split("/");
					   Set<String> set = new HashSet<String>(Arrays.asList(arr));
					   String result = String.join("/", set);
					   scoreTwoStatisticsexcel.setRefereeRemark(result);
				}
				//三个裁判
			   if (stuList.get(i).getMarkVO().size() == 3){
				  for (int p = 0;p < stuList.get(i).getMarkVO().size();p++){
					 if (p == 0){
						scoreThreeStatisticsexcel.setRefeereOneScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					 }else if (p == 1){
						scoreThreeStatisticsexcel.setRefeereTwoScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					 }else if (p == 2){
						scoreThreeStatisticsexcel.setRefeereThreeScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					 }
				  }
				   scoreThreeStatisticsexcel.setEncryptionUmber(stuList.get(i).getRealityNm1());
				   scoreThreeStatisticsexcel.setEncryptionUmber2(stuList.get(i).getRealityNm2());
				   scoreThreeStatisticsexcel.setUnitName(stuList.get(i).getUnitName());
				   scoreThreeStatisticsexcel.setName(stuList.get(i).getName());
				   scoreThreeStatisticsexcel.setIdNumber(stuList.get(i).getIdNumber());
				   scoreThreeStatisticsexcel.setTotalScore(stuList.get(i).getTotalScore());
				   String[] arr = stuList.get(i).getRefereeremark().split("/");
				   Set<String> set = new HashSet<String>(Arrays.asList(arr));
				   String result = String.join("/", set);
				   scoreThreeStatisticsexcel.setRefereeRemark(result);
			   }
			   //四个裁判
			   if (stuList.get(i).getMarkVO().size() == 4){
				  for (int p = 0;p < stuList.get(i).getMarkVO().size();p++){
					 if (p == 0){
						scoreFourStatisticsexcel.setRefeereOneScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					 }else if (p == 1){
						scoreFourStatisticsexcel.setRefeereTwoScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					 }else if (p == 2){
						scoreFourStatisticsexcel.setRefeereThreeScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					 }else if (p == 3){
						scoreFourStatisticsexcel.setRefeereFourScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					 }
				  }
				   scoreFourStatisticsexcel.setEncryptionUmber(stuList.get(i).getRealityNm1());
				   scoreFourStatisticsexcel.setEncryptionUmber2(stuList.get(i).getRealityNm2());
				   scoreFourStatisticsexcel.setUnitName(stuList.get(i).getUnitName());
				   scoreFourStatisticsexcel.setName(stuList.get(i).getName());
				   scoreFourStatisticsexcel.setIdNumber(stuList.get(i).getIdNumber());
				   scoreFourStatisticsexcel.setTotalScore(stuList.get(i).getTotalScore());
				   String[] arr = stuList.get(i).getRefereeremark().split("/");
				   Set<String> set = new HashSet<String>(Arrays.asList(arr));
				   String result = String.join("/", set);
				   scoreFourStatisticsexcel.setRefereeRemark(result);
			   }
		   //五个裁判
		   if (stuList.get(i).getMarkVO().size() == 5){
				  for (int p = 0;p < stuList.get(i).getMarkVO().size();p++){
					 if (p == 0){
						scoreFiveStatisticsexcel.setRefeereOneScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					 }else if (p == 1){
						scoreFiveStatisticsexcel.setRefeereTwoScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					 }else if (p == 2){
						 scoreFiveStatisticsexcel.setRefeereThreeScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					 }else if (p == 3){
						 scoreFiveStatisticsexcel.setRefeereFourScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					 }else if(p == 4){
						 scoreFiveStatisticsexcel.setRefeereFiveScore(stuList.get(i).getMarkVO().get(p).getRefereeScore());
					 }
				  }
				   scoreFiveStatisticsexcel.setEncryptionUmber(stuList.get(i).getRealityNm1());
				   scoreFiveStatisticsexcel.setEncryptionUmber2(stuList.get(i).getRealityNm2());
				   scoreFiveStatisticsexcel.setUnitName(stuList.get(i).getUnitName());
				   scoreFiveStatisticsexcel.setName(stuList.get(i).getName());
				   scoreFiveStatisticsexcel.setIdNumber(stuList.get(i).getIdNumber());
				   scoreFiveStatisticsexcel.setTotalScore(stuList.get(i).getTotalScore());
				   String[] arr = stuList.get(i).getRefereeremark().split("/");
				   Set<String> set = new HashSet<String>(Arrays.asList(arr));
				   String result = String.join("/", set);
				   scoreFiveStatisticsexcel.setRefereeRemark(result);
			  }
			excelList.add(ExcelMap);
			excelOne.add(scoreOneStatisticsexcel);
			excelTwo.add(scoreTwoStatisticsexcel);
			excelThree.add(scoreThreeStatisticsexcel);
			excelFour.add(scoreFourStatisticsexcel);
			excelFive.add(scoreFiveStatisticsexcel);
		}
		for (int i = 0;i<stuList.size();i++){
			if (stuList.get(i).getMarkVO().size() == 1){
				EasyPoiUtil.exportExcel(excelOne, competitionName+"赛场成绩导出",  competitionName+"赛场成绩导出", ScoreOneStatisticsexcel.class, competitionName+"赛场成绩导出", response);
			}else if (stuList.get(i).getMarkVO().size() == 2){
				EasyPoiUtil.exportExcel(excelTwo, competitionName+"赛场成绩导出",  competitionName+"赛场成绩导出", ScoreTwoStatisticsexcel.class, competitionName+"赛场成绩导出", response);
			}else if (stuList.get(i).getMarkVO().size() == 3){
				EasyPoiUtil.exportExcel(excelThree, competitionName+"赛场成绩导出",  competitionName+"赛场成绩导出", ScoreThreeStatisticsexcel.class, competitionName+"赛场成绩导出", response);
			}else if (stuList.get(i).getMarkVO().size() == 4){
				EasyPoiUtil.exportExcel(excelFour, competitionName+"赛场成绩导出",  competitionName+"赛场成绩导出", ScoreFourStatisticsexcel.class, competitionName+"赛场成绩导出", response);
			}else if (stuList.get(i).getMarkVO().size() == 5){
				EasyPoiUtil.exportExcel(excelFive, competitionName+"赛场成绩导出",  competitionName+"赛场成绩导出", ScoreFiveStatisticsexcel.class, competitionName+"赛场成绩导出", response);
			}
		}
		return Result.ok("导出成功！请勿关闭浏览器,五分钟后浏览！");
	}


	/** 成绩总统计导出 */
	@ResponseBody
	@GetMapping("/personageStatisticsexcel")
	public  Result  personageStatisticsexcel(Long cbatchId,HttpServletResponse response){
		List<MarkStatisticsExcel> list = stuSignUpService.personageStatisticsexcel(cbatchId, response);
		EasyPoiUtil.exportExcel(list, "成绩总统计导出",  "成绩总统计导出", MarkStatisticsExcel.class, "成绩总统计导出", response);

		return Result.ok("导出成功!");
	}
	/** 裁判打分处理  */
	private void  dateHandleExcel (List<StuSignUpComDO> stuList,List<MarkVO> secureMarkVOS,String competition) {
		for (int i = 0; i < stuList.size(); i++) {
			ArrayList<MarkVO> MarkVO = new ArrayList<>();
			for (int k = 0; k < secureMarkVOS.size(); k++) {
				if (stuList.get(i).getId().equals(secureMarkVOS.get(k).getSignId())) {//用户表学员表与打分表考试id匹配
					//计算裁判打的这个任务的总分
					String[] refereeScores = secureMarkVOS.get(k).getRefereeScore().split("/");//分割出来
					BigDecimal refereeScore = new BigDecimal(0);
					for (String score : refereeScores) {
						refereeScore = new BigDecimal(score).add(refereeScore);//得到所有裁判打的总分
					}
					secureMarkVOS.get(k).setRefereeScore(String.valueOf(refereeScore));//将裁判打的这个任务的总分存入这个对象中（这里就是这个裁判打的这个任务的总分）
					//计算出这个学员这个任务中所获得的总分（每个裁判总分除以裁判数）//这里k等于几就是有几个裁判
					//如果相同那么将这条数据添加到用户集合对象中去
					System.out.println("对象：" + secureMarkVOS.get(k));
					MarkVO.add(secureMarkVOS.get(k));
				}
			}
			stuList.get(i).setMarkVO(MarkVO);
			stuList.get(i).getMarkVO().size();//有多少条数就有多少个裁判
			Double TotalScore = 0.0;
			for (int j = 0; j < stuList.get(i).getMarkVO().size(); j++) {//这个学员的所有裁判打的安全防护分
				TotalScore = Double.parseDouble(stuList.get(i).getMarkVO().get(j).getRefereeScore()) + TotalScore;//得到所有裁判打的总分（这里因为是个人总统计，所以这里是B赛场这个任务所得的总分）
			}
			//这里传入对应的参数，判断是那个赛场的得分
			if (competition.equals("安全防护")){//获得该学员安全防护的得分
				//最终学员这个任务所获得的总分（这里是B赛场安全防护得分），因为是要最终这个学员三个赛场的总得分所以要加上另外两个赛场的得分
				//最终学员这个任务所获得的总分
				if (TotalScore <= 0){
					stuList.get(i).setSecureScore(0.0);
				}else {
					BigDecimal bg = new BigDecimal(TotalScore / stuList.get(i).getMarkVO().size());
					stuList.get(i).setSecureScore( bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());//最终得分为每个裁判打的总分除以裁判人数
				}
			}else if(competition.equals("日常保健")){//获得该学员的日常保健的得分
				if (TotalScore <= 0){
					stuList.get(i).setHealthScore(0.0);
				}else {
					stuList.get(i).setHealthScore(new BigDecimal(TotalScore / stuList.get(i).getMarkVO().size()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
				}
			}else if (competition.equals("生活照料")){
				if (TotalScore <= 0){
					stuList.get(i).setLookAfterScore(0.0);
				}else {
					stuList.get(i).setLookAfterScore(new BigDecimal(TotalScore / stuList.get(i).getMarkVO().size()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
				}
			}else if (competition.equals("早期发展指导")){
				if (TotalScore <= 0){
					stuList.get(i).setDevelopmentGuidanceScore(0.0);
				}else {
					stuList.get(i).setDevelopmentGuidanceScore(new BigDecimal(TotalScore / stuList.get(i).getMarkVO().size()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
				}
			}

		}
	}


	//数据流去重
	public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
		Map<Object,Boolean> seen = new ConcurrentHashMap<>();
		return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
	}




	/**
	 *安全防护赛场成绩统计（BC赛场下的所有安全返回的赛场成绩统计）
	 * */
	@ApiOperation(value = "安全防护赛场成绩统计", notes = "安全防护赛场成绩统计")
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@PostMapping("/secureList")
	@ResponseBody
	public Result<?>  secureList(@RequestBody ExaminationsVo examinationsVo){
		StatisticsVO statisticsVO = new StatisticsVO();
		ArrayList<Long>	ids	=	new ArrayList<>();
		ArrayList<Long>	stuIds	=	new ArrayList<>();
		//查询出实操赛场学员信息
		//查询这个赛场下的模板任务
		  //赛场数据
			//如果批次下面没有赛场
		ExaminationBatchComSDO examinationBatchComSDO = examinationBatchComSService.selectById(examinationsVo.getCompetitionId());
		if (examinationBatchComSDO == null){
			Page<StuSignUpComDO> stuList = new Page<>();
			statisticsVO.setStuSignUpComDO(stuList);
			 return Result.ok(statisticsVO);
		}
		 //查询对应的模板数据
		EntityWrapper<JzJinzhuanDispositionTopic> wrapperTopic = new EntityWrapper<>();
		wrapperTopic.eq("type","B");//赛场（B）			//模板id
		wrapperTopic.eq("module_id",examinationBatchComSDO.getModileId());
		List<JzJinzhuanDispositionTopic> topicList = dispositionTopicService.selectList(wrapperTopic);//B赛场所有模板任务
		if (topicList.size() <= 0){
			Page<StuSignUpComDO> stuList = new Page<>();
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		for (int i = 0;i<topicList.size();i++){
			if (topicList.get(i).getTopicName().contains("安全防护")){//如果相等就是安全防护任务
				//模板id
				 ids.add(topicList.get(i).getId());
			}
		}
		StuSignUpComDO stuSignUpComDO = new StuSignUpComDO();
		stuSignUpComDO.setEbatchId(examinationsVo.getEbatchId());
		stuSignUpComDO.setCbatchId(examinationsVo.getCbatchId());
		stuSignUpComDO.setCompetition("B");
		stuSignUpComDO.setJinzhuanTopicId(String.valueOf(ids.get(0)));
		stuSignUpComDO.setCompetitionId(examinationsVo.getCompetitionId());//赛场id
		stuSignUpComDO.setRaceId(examinationBatchComSDO.getId());
		Page<StuSignUpComDO> stuList = stuSignUpService.ListUstASC(examinationsVo.getCurrent(), examinationsVo.getNumber(), stuSignUpComDO);
		List<StuSignUpComDO> ss = stuList.getRecords().stream().filter(a -> Objects.nonNull(a.getId())).collect(Collectors.toList());
		stuList.setRecords(ss);
		if (stuList.getRecords().size() <= 0){
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		for (int i = 0 ;i < stuList.getRecords().size(); i++){
			//考生id
			stuIds.add(stuList.getRecords().get(i).getId());
		}
		//查询出打分信息
		Map map =new HashMap();
		map.put("competition","B");
		map.put("signId",stuIds);
		map.put("jinzhuanTopicId",ids);
		map.put("cbatchId",examinationsVo.getCbatchId());
		map.put("ebatchId",examinationsVo.getEbatchId());
		map.put("race_id",examinationBatchComSDO.getId());
		//查询出裁判数量
		List<MarkVO> mrkRefereeVOS = markService.selectRefereeBCSize(map);
		//裁判打分数据
		List<MarkVO> markVOS = markService.seleScoreTopicContent(map);
		if (markVOS.size() <= 0){
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		//进行学员匹配
		for (int i = 0;i<stuList.getRecords().size();i++){
			ArrayList<MarkVO>	MarkVOlist	=	new ArrayList<>();
			for (int k = 0;k<markVOS.size();k++){
				if (stuList.getRecords().get(i).getId().equals(markVOS.get(k).getSignId())) {//用户表学员表与打分表考试id匹配
					//计算裁判打的这个任务的总分
					String[] refereeScores = markVOS.get(k).getRefereeScore().split("/");//分割出来
					Double refereeScore = 0.0;
					for (String score : refereeScores) {
						refereeScore = Double.valueOf(score) + refereeScore;//两个人分数相加（计算）
					}
					markVOS.get(k).setRefereeScore(String.valueOf(refereeScore));//将裁判打的这个任务的总分存入这个对象中
					//计算出这个学员这个任务中所获得的总分（每个裁判总分除以裁判数）//这里k等于几就是有几个裁判
					//如果相同那么将这条数据添加到用户集合对象中去
					System.out.println("对象："+markVOS.get(k));
					MarkVOlist.add(markVOS.get(k));
					//将批次和学员id返回到学生信息中
					stuList.getRecords().get(i).setCbatchId(markVOS.get(k).getCbatchId());
					stuList.getRecords().get(i).setSignId(markVOS.get(k).getSignId());
				}
			}
				//将两个集合合并
				List<MarkVO> resultList = Stream.concat(MarkVOlist.stream(), mrkRefereeVOS.stream())
						.collect(Collectors.toList());
				//去掉重复的裁判数据
				List<MarkVO> resultListDuplicate = resultList.stream().filter(distinctByKey(b->b.getRefereeId())).collect(Collectors.toList());
				if(resultList.size() > 0){
					stuList.getRecords().get(i).setMarkVO(resultListDuplicate);
					stuList.getRecords().get(i).getMarkVO().size();//有多少条数就有多少个裁判
					BigDecimal TotalScore = new BigDecimal(0);
					for (int j = 0; j< stuList.getRecords().get(i).getMarkVO().size();j++){//这个学员的所有裁判打的安全防护分
						if (!stuList.getRecords().get(i).getMarkVO().get(j).getRefereeScore().equals("-")){
							TotalScore = new BigDecimal(stuList.getRecords().get(i).getMarkVO().get(j).getRefereeScore()).add(TotalScore);//得到所有裁判打的总分add是相加
						}
					}
					if (TotalScore.compareTo(BigDecimal.ZERO)!=0){
						BigDecimal size = new BigDecimal(stuList.getRecords().get(i).getMarkVO().size());
						BigDecimal totalScore = TotalScore.divide(size,2,BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);//divide是除以
						//最终学员这个任务所获得的总分
						stuList.getRecords().get(i).setTotalScore(totalScore.doubleValue());//最终得分为每个裁判打的总分除以裁判人数
					}else if (TotalScore.compareTo(BigDecimal.ZERO)==0){
						BigDecimal size = new BigDecimal(stuList.getRecords().get(i).getMarkVO().size());
						BigDecimal totalScore = TotalScore.divide(size);
						stuList.getRecords().get(i).setTotalScore(totalScore.doubleValue());//最终得分为每个裁判打的总分除以裁判人数
					}
				}
		}
		//根据总分进行排序
		Collections.sort(stuList.getRecords(),Comparator.comparing(StuSignUpComDO::getTotalScore).reversed());
		//赛场人数
		statisticsVO.setStuNumber(stuList.getRecords().size());
		//最高分
		Double maxScore = 0.0;
		for (int i = 0 ;i < stuList.getRecords().size() ; i++){
					if (stuList.getRecords().get(i).getTotalScore() != null && stuList.getRecords().get(i).getTotalScore() > maxScore){
						maxScore = stuList.getRecords().get(i).getTotalScore();
					}
		}
		statisticsVO.setMaxScore(maxScore);
		//最低分
		Double minScore = 100.0;
		for (int i = 0 ;i < stuList.getRecords().size() ; i++){
			if (stuList.getRecords().get(i).getTotalScore() != null && stuList.getRecords().get(i).getTotalScore() < minScore){
				minScore = stuList.getRecords().get(i).getTotalScore();
			}else if (minScore == 0.0){
				minScore = 0.0;
			}
		}
		statisticsVO.setMinScore(minScore);
		//将用户分数对象存入最终对象里面去
		statisticsVO.setStuSignUpComDO(stuList);
//		statisticsVO.getStuSignUpComDO().getSize();
//		statisticsVO.getStuSignUpComDO().getTotal();
		statisticsVO.setSize(statisticsVO.getStuSignUpComDO().getSize());
		statisticsVO.setTotal(statisticsVO.getStuSignUpComDO().getTotal());
		return Result.ok(statisticsVO);
	}


	/**
	 *日常保健赛场成绩统计
	 * */
	@ApiOperation(value = "日常保健赛场成绩统计", notes = "日常保健赛场成绩统计")
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@PostMapping("/healthList")
	@ResponseBody
	public Result<?>  healthList(@RequestBody ExaminationsVo examinationsVo){
		StatisticsVO statisticsVO = new StatisticsVO();
		ArrayList<Long>	ids	=	new ArrayList<>();
		ArrayList<Long>	stuIds	=	new ArrayList<>();
		//查询出实操赛场学员信息
		//赛场数据
		ExaminationBatchComSDO examinationBatchComSDO = examinationBatchComSService.selectById(examinationsVo.getCompetitionId());
		if (examinationBatchComSDO == null){
			Page<StuSignUpComDO> stuList = new Page<>();
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		//查询模板任务
		EntityWrapper<JzJinzhuanDispositionTopic> wrapperTopic = new EntityWrapper<>();
		wrapperTopic.eq("type","B");//赛场（B）
		wrapperTopic.eq("module_id",examinationBatchComSDO.getModileId());//模板id
		List<JzJinzhuanDispositionTopic> topicList = dispositionTopicService.selectList(wrapperTopic);//B赛场所有模板任务
		if (topicList.size() <= 0){
			Page<StuSignUpComDO> stuList = new Page<>();
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		for (int i = 0;i<topicList.size();i++){
			if (topicList.get(i).getTopicName().contains("日常保健")){//如果相等就是安全防护任务
				//模板id
				ids.add(topicList.get(i).getId());
			}
		}
		StuSignUpComDO stuSignUpComDO = new StuSignUpComDO();
		stuSignUpComDO.setCbatchId(examinationsVo.getCbatchId());
		stuSignUpComDO.setEbatchId(examinationsVo.getEbatchId());
		stuSignUpComDO.setCompetition("B");
		stuSignUpComDO.setJinzhuanTopicId(String.valueOf(ids.get(0)));
		stuSignUpComDO.setCompetitionId(examinationsVo.getCompetitionId());//赛场id
		stuSignUpComDO.setRaceId(examinationBatchComSDO.getId());
		Page<StuSignUpComDO> stuList = stuSignUpService.ListUstASC(examinationsVo.getCurrent(), examinationsVo.getNumber(), stuSignUpComDO);
		if (stuList.getRecords().size() <= 0){
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		List<StuSignUpComDO> ss = stuList.getRecords().stream().filter(a -> Objects.nonNull(a.getId())).collect(Collectors.toList());
		stuList.setRecords(ss);
		for (int i = 0 ;i < stuList.getRecords().size(); i++){
			//考生id
			stuIds.add(stuList.getRecords().get(i).getId());
		}
		//查询出打分信息
		Map map =new HashMap();
		map.put("competition","B");
		map.put("signId",stuIds);
		map.put("jinzhuanTopicId",ids);
		map.put("cbatchId",examinationsVo.getCbatchId());
		map.put("ebatchId",examinationsVo.getEbatchId());
		map.put("race_id",examinationBatchComSDO.getId());
		//查询出裁判数量
		List<MarkVO> mrkRefereeVOS = markService.selectRefereeBCSize(map);
		//裁判打分数据
		List<MarkVO> markVOS = markService.seleScoreTopicContent(map);
		if (markVOS.size() <= 0){
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		//进行学员匹配
		for (int i = 0;i<stuList.getRecords().size();i++){
			ArrayList<MarkVO>	MarkVOlist	=	new ArrayList<>();
			for (int k = 0;k<markVOS.size();k++){
				if (stuList.getRecords().get(i).getId().equals(markVOS.get(k).getSignId())) {//用户表学员表与打分表考试id匹配
					//计算裁判打的这个任务的总分
					String[] refereeScores = markVOS.get(k).getRefereeScore().split("/");//分割出来
					Double refereeScore = 0.0;
					for (String score : refereeScores) {
						refereeScore = Double.valueOf(score) + refereeScore;//两个人分数相加（计算）
					}
					markVOS.get(k).setRefereeScore(String.valueOf(refereeScore));//将裁判打的这个任务的总分存入这个对象中
					//计算出这个学员这个任务中所获得的总分（每个裁判总分除以裁判数）//这里k等于几就是有几个裁判
					//如果相同那么将这条数据添加到用户集合对象中去
					System.out.println("对象："+markVOS.get(k));
					MarkVOlist.add(markVOS.get(k));
					stuList.getRecords().get(i).setCbatchId(markVOS.get(k).getCbatchId());
					stuList.getRecords().get(i).setSignId(markVOS.get(k).getSignId());
				}
			}
			//将两个集合合并
			List<MarkVO> resultList = Stream.concat(MarkVOlist.stream(), mrkRefereeVOS.stream())
					.collect(Collectors.toList());
			//去掉重复的裁判数据
			List<MarkVO> resultListDuplicate = resultList.stream().filter(distinctByKey(b->b.getRefereeId())).collect(Collectors.toList());
			if (resultList.size() > 0){
				stuList.getRecords().get(i).setMarkVO(resultListDuplicate);
				stuList.getRecords().get(i).getMarkVO().size();//有多少条数就有多少个裁判
				BigDecimal TotalScore = new BigDecimal(0);
				for (int j = 0; j< stuList.getRecords().get(i).getMarkVO().size();j++){//这个学员的所有裁判打的安全防护分
					if (!stuList.getRecords().get(i).getMarkVO().get(j).getRefereeScore().equals("-")){
						TotalScore = new BigDecimal(stuList.getRecords().get(i).getMarkVO().get(j).getRefereeScore()).add(TotalScore);//得到所有裁判打的总分
					}
				}
				if (TotalScore.compareTo(BigDecimal.ZERO)!=0){
					BigDecimal size = new BigDecimal(stuList.getRecords().get(i).getMarkVO().size());
					BigDecimal totalScore = TotalScore.divide(size,2,BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);//divide是除以
					//最终学员这个任务所获得的总分
					stuList.getRecords().get(i).setTotalScore(totalScore.doubleValue());//最终得分为每个裁判打的总分除以裁判人数
				}else if (TotalScore.compareTo(BigDecimal.ZERO)==0){
					BigDecimal size = new BigDecimal(stuList.getRecords().get(i).getMarkVO().size());
					BigDecimal totalScore = TotalScore.divide(size);
					stuList.getRecords().get(i).setTotalScore(totalScore.doubleValue());//最终得分为每个裁判打的总分除以裁判人数
				}
			}
		}
		//根据总分进行排序
		Collections.sort(stuList.getRecords(),Comparator.comparing(StuSignUpComDO::getTotalScore).reversed());
		//赛场人数
		statisticsVO.setStuNumber(stuList.getRecords().size());
		//最高分
		Double maxScore = 0.0;
		for (int i = 0 ;i < stuList.getRecords().size() ; i++){
			if (stuList.getRecords().get(i).getTotalScore() != null && stuList.getRecords().get(i).getTotalScore() > maxScore){
				maxScore = stuList.getRecords().get(i).getTotalScore();
			}
		}
		statisticsVO.setMaxScore(maxScore);
		//最低分
		Double minScore = 100.0;
		for (int i = 0 ;i < stuList.getRecords().size() ; i++){
			if (stuList.getRecords().get(i).getTotalScore() != null && stuList.getRecords().get(i).getTotalScore() < minScore){
				minScore = stuList.getRecords().get(i).getTotalScore();
			}else if (minScore == 0.0){
				minScore = 0.0;
			}
		}
		statisticsVO.setMinScore(minScore);
		//将用户分数对象存入最终对象里面去
		statisticsVO.setStuSignUpComDO(stuList);
		statisticsVO.setSize(statisticsVO.getStuSignUpComDO().getSize());
		statisticsVO.setTotal(statisticsVO.getStuSignUpComDO().getTotal());
		return Result.ok(statisticsVO);
	}


	/**
	 * 生活照料赛场成绩统计
	 * */
	@ApiOperation(value = "生活照料导赛场成绩统计", notes = "生活照料导赛场成绩统计")
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@PostMapping("/lookAfterList")
	@ResponseBody
	public Result<?>  lookAfterList(@RequestBody ExaminationsVo examinationsVo){
		StatisticsVO statisticsVO = new StatisticsVO();
		ArrayList<Long>	ids	=	new ArrayList<>();
		ArrayList<Long>	stuIds	=	new ArrayList<>();
		//查询出实操赛场学员信息
		//赛场数据
		ExaminationBatchComSDO examinationBatchComSDO = examinationBatchComSService.selectById(examinationsVo.getCompetitionId());
		if (examinationBatchComSDO == null){
			Page<StuSignUpComDO> stuList = new Page<>();
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		//查询模板任务
		EntityWrapper<JzJinzhuanDispositionTopic> wrapperTopic = new EntityWrapper<>();
		wrapperTopic.eq("type","C");//赛场（B）
		wrapperTopic.eq("module_id",examinationBatchComSDO.getModileId());//模板id
		List<JzJinzhuanDispositionTopic> topicList = dispositionTopicService.selectList(wrapperTopic);//B赛场所有模板任务
		if (topicList.size() <= 0){
			Page<StuSignUpComDO> stuList = new Page<>();
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		for (int i = 0;i<topicList.size();i++){
			if (topicList.get(i).getTopicName().contains("生活照料")){//如果相等就是安全防护任务
				//模板id
				ids.add(topicList.get(i).getId());
			}
		}
		StuSignUpComDO stuSignUpComDO = new StuSignUpComDO();
		stuSignUpComDO.setCbatchId(examinationsVo.getCbatchId());
		stuSignUpComDO.setEbatchId(examinationsVo.getEbatchId());
		stuSignUpComDO.setCompetition("C");
		stuSignUpComDO.setJinzhuanTopicId(String.valueOf(ids.get(0)));
		stuSignUpComDO.setCompetitionId(examinationsVo.getCompetitionId());//赛场id
		stuSignUpComDO.setRaceId(examinationBatchComSDO.getId());
		Page<StuSignUpComDO> stuList = stuSignUpService.ListUstASC(examinationsVo.getCurrent(), examinationsVo.getNumber(), stuSignUpComDO);
		List<StuSignUpComDO> ss = stuList.getRecords().stream().filter(a -> Objects.nonNull(a.getId())).collect(Collectors.toList());
		stuList.setRecords(ss);
		if (stuList.getRecords().size() <= 0){
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		for (int i = 0 ;i < stuList.getRecords().size(); i++){
			//考生id
			stuIds.add(stuList.getRecords().get(i).getId());
		}
		//查询出打分信息
		Map map =new HashMap();
		map.put("competition","C");
		map.put("signId",stuIds);
		map.put("jinzhuanTopicId",ids);
		map.put("cbatchId",examinationsVo.getCbatchId());
		map.put("ebatchId",examinationsVo.getEbatchId());
		map.put("race_id",examinationBatchComSDO.getId());
		//查询出裁判数量
		List<MarkVO> mrkRefereeVOS = markService.selectRefereeBCSize(map);
		//裁判打分数据
		List<MarkVO> markVOS = markService.seleScoreTopicContent(map);
		if (markVOS.size() <= 0){
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		//进行学员匹配
		for (int i = 0;i<stuList.getRecords().size();i++){
			ArrayList<MarkVO>	MarkVOlist	=	new ArrayList<>();
			for (int k = 0;k<markVOS.size();k++){
				if (stuList.getRecords().get(i).getId().equals(markVOS.get(k).getSignId())) {//用户表学员表与打分表考试id匹配
					//计算裁判打的这个任务的总分
					String[] refereeScores = markVOS.get(k).getRefereeScore().split("/");//分割出来
					Double refereeScore = 0.0;
					for (String score : refereeScores) {
						refereeScore = Double.valueOf(score) + refereeScore;//两个人分数相加（计算）
					}
					markVOS.get(k).setRefereeScore(String.valueOf(refereeScore));//将裁判打的这个任务的总分存入这个对象中
					//计算出这个学员这个任务中所获得的总分（每个裁判总分除以裁判数）//这里k等于几就是有几个裁判
					//如果相同那么将这条数据添加到用户集合对象中去
					System.out.println("对象："+markVOS.get(k));
					MarkVOlist.add(markVOS.get(k));
					stuList.getRecords().get(i).setCbatchId(markVOS.get(k).getCbatchId());
					stuList.getRecords().get(i).setSignId(markVOS.get(k).getSignId());
				}
			}
			//将两个集合合并
			List<MarkVO> resultList = Stream.concat(MarkVOlist.stream(), mrkRefereeVOS.stream())
					.collect(Collectors.toList());
			//去掉重复的裁判数据
			List<MarkVO> resultListDuplicate = resultList.stream().filter(distinctByKey(b->b.getRefereeId())).collect(Collectors.toList());
			if(resultListDuplicate.size() > 0){
				stuList.getRecords().get(i).setMarkVO(resultListDuplicate);
				stuList.getRecords().get(i).getMarkVO().size();//有多少条数就有多少个裁判
				BigDecimal TotalScore = new BigDecimal(0);
				for (int j = 0; j< stuList.getRecords().get(i).getMarkVO().size();j++){//这个学员的所有裁判打的安全防护分
					if (!stuList.getRecords().get(i).getMarkVO().get(j).getRefereeScore().equals("-")){
						TotalScore = new BigDecimal(stuList.getRecords().get(i).getMarkVO().get(j).getRefereeScore()).add(TotalScore);//得到所有裁判打的总分
					}
				}
				BigDecimal size = new BigDecimal(stuList.getRecords().get(i).getMarkVO().size());
				BigDecimal totalScore = TotalScore.divide(size,2,BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
				//最终学员这个任务所获得的总分
				stuList.getRecords().get(i).setTotalScore(totalScore.doubleValue());//最终得分为每个裁判打的总分除以裁判人数
			}
		}
		//根据总分进行排序
		Collections.sort(stuList.getRecords(),Comparator.comparing(StuSignUpComDO::getTotalScore).reversed());
		//赛场人数
		statisticsVO.setStuNumber(stuList.getRecords().size());
		//最高分
		Double maxScore = 0.0;
		for (int i = 0 ;i < stuList.getRecords().size() ; i++){
			if (stuList.getRecords().get(i).getTotalScore() != null && stuList.getRecords().get(i).getTotalScore() > maxScore){
				maxScore = stuList.getRecords().get(i).getTotalScore();
			}
		}
		statisticsVO.setMaxScore(maxScore);
		//最低分
		Double minScore = 100.0;
		for (int i = 0 ;i < stuList.getRecords().size() ; i++){
			if (stuList.getRecords().get(i).getTotalScore() != null && stuList.getRecords().get(i).getTotalScore() < minScore){
				minScore = stuList.getRecords().get(i).getTotalScore();
			}else if (minScore == 0.0){
				minScore = 0.0;
			}
		}
		statisticsVO.setMinScore(minScore);
		//将用户分数对象存入最终对象里面去
		statisticsVO.setStuSignUpComDO(stuList);
		statisticsVO.setSize(statisticsVO.getStuSignUpComDO().getSize());
		statisticsVO.setTotal(statisticsVO.getStuSignUpComDO().getTotal());
		return Result.ok(statisticsVO);
	}





	/**
	 *早期发展指导赛场成绩统计
	 * */
	@ApiOperation(value = "早期发展指导赛场成绩统计", notes = "早期发展指导赛场成绩统计")
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@PostMapping("/developmentGuidanceList")
	@ResponseBody
	public Result<?>  developmentGuidanceList(@RequestBody ExaminationsVo examinationsVo){
		StatisticsVO statisticsVO = new StatisticsVO();
		ArrayList<Long>	ids	=	new ArrayList<>();
		ArrayList<Long>	stuIds	=	new ArrayList<>();
		//查询出实操赛场学员信息
		//赛场数据
		ExaminationBatchComSDO examinationBatchComSDO = examinationBatchComSService.selectById(examinationsVo.getCompetitionId());
		if (examinationBatchComSDO == null){
			Page<StuSignUpComDO> stuList = new Page<>();
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		//查询模板任务
		EntityWrapper<JzJinzhuanDispositionTopic> wrapperTopic = new EntityWrapper<>();
		wrapperTopic.eq("type","C");//赛场（B）			//模板id
		wrapperTopic.eq("module_id",examinationBatchComSDO.getModileId());//模板id
		List<JzJinzhuanDispositionTopic> topicList = dispositionTopicService.selectList(wrapperTopic);//B赛场所有模板任务
		if (topicList.size() <= 0){
			Page<StuSignUpComDO> stuList = new Page<>();
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		for (int i = 0;i<topicList.size();i++){
			if (topicList.get(i).getTopicName().contains("早期发展指导")){//如果相等就是安全防护任务
				//模板id
				ids.add(topicList.get(i).getId());
			}
		}
		StuSignUpComDO stuSignUpComDO = new StuSignUpComDO();
		stuSignUpComDO.setCbatchId(examinationsVo.getCbatchId());
		stuSignUpComDO.setEbatchId(examinationsVo.getEbatchId());
		stuSignUpComDO.setCompetition("C");
		stuSignUpComDO.setJinzhuanTopicId(String.valueOf(ids.get(0)));
		stuSignUpComDO.setCompetitionId(examinationsVo.getCompetitionId());//赛场id
		stuSignUpComDO.setRaceId(examinationBatchComSDO.getId());
		Page<StuSignUpComDO> stuList = stuSignUpService.ListUstASC(examinationsVo.getCurrent(), examinationsVo.getNumber(), stuSignUpComDO);
		List<StuSignUpComDO> ss = stuList.getRecords().stream().filter(a -> Objects.nonNull(a.getId())).collect(Collectors.toList());
		stuList.setRecords(ss);
		if (stuList.getRecords().size() <= 0){
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		for (int i = 0 ;i < stuList.getRecords().size(); i++){
			//考生id
			stuIds.add(stuList.getRecords().get(i).getId());
		}
		//查询出打分信息
		Map map =new HashMap();
		map.put("competition","C");
		map.put("signId",stuIds);
		map.put("jinzhuanTopicId",ids);
		map.put("cbatchId",examinationsVo.getCbatchId());
		map.put("ebatchId",examinationsVo.getEbatchId());
		map.put("race_id",examinationBatchComSDO.getId());
		//查询出裁判数量
		List<MarkVO> mrkRefereeVOS = markService.selectRefereeBCSize(map);
		//裁判打分数据
		List<MarkVO> markVOS = markService.seleScoreTopicContent(map);
		if(markVOS.size() <= 0){
			statisticsVO.setStuSignUpComDO(stuList);
			return Result.ok(statisticsVO);
		}
		//进行学员匹配
		for (int i = 0;i<stuList.getRecords().size();i++){
			ArrayList<MarkVO>	MarkVOlist	=	new ArrayList<>();
			for (int k = 0;k<markVOS.size();k++){
				if (stuList.getRecords().get(i).getId().equals(markVOS.get(k).getSignId())) {//用户表学员表与打分表考试id匹配
					//计算裁判打的这个任务的总分
					String[] refereeScores = markVOS.get(k).getRefereeScore().split("/");//分割出来
					Double refereeScore = 0.0;
					for (String score : refereeScores) {
						refereeScore = Double.valueOf(score) + refereeScore;//两个人分数相加（计算）
					}
					markVOS.get(k).setRefereeScore(String.valueOf(refereeScore));//将裁判打的这个任务的总分存入这个对象中
					//计算出这个学员这个任务中所获得的总分（每个裁判总分除以裁判数）//这里k等于几就是有几个裁判
					//如果相同那么将这条数据添加到用户集合对象中去
					System.out.println("对象："+markVOS.get(k));
					MarkVOlist.add(markVOS.get(k));
					stuList.getRecords().get(i).setCbatchId(markVOS.get(k).getCbatchId());
					stuList.getRecords().get(i).setSignId(markVOS.get(k).getSignId());
				}
			}
			//将两个集合合并
			List<MarkVO> resultList = Stream.concat(MarkVOlist.stream(), mrkRefereeVOS.stream())
					.collect(Collectors.toList());
			//去掉重复的裁判数据
			List<MarkVO> resultListDuplicate = resultList.stream().filter(distinctByKey(b->b.getRefereeId())).collect(Collectors.toList());
			if (resultList.size() > 0){
				stuList.getRecords().get(i).setMarkVO(resultListDuplicate);
				stuList.getRecords().get(i).getMarkVO().size();//有多少条数就有多少个裁判
				BigDecimal TotalScore = new BigDecimal(0);
				for (int j = 0; j< stuList.getRecords().get(i).getMarkVO().size();j++){//这个学员的所有裁判打的安全防护分
					if (!stuList.getRecords().get(i).getMarkVO().get(j).getRefereeScore().equals("-")){
						TotalScore = new BigDecimal(stuList.getRecords().get(i).getMarkVO().get(j).getRefereeScore()).add(TotalScore);//得到所有裁判打的总分
					}
				}
				BigDecimal size = new BigDecimal(stuList.getRecords().get(i).getMarkVO().size());
				BigDecimal totalScore = TotalScore.divide(size,2,BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
				//最终学员这个任务所获得的总分
				stuList.getRecords().get(i).setTotalScore(totalScore.doubleValue());//最终得分为每个裁判打的总分除以裁判人数
			}
		}
		//根据总分进行排序
		Collections.sort(stuList.getRecords(),Comparator.comparing(StuSignUpComDO::getTotalScore).reversed());
		//赛场人数
		statisticsVO.setStuNumber(stuList.getRecords().size());
		//最高分
		Double maxScore = 0.0;
		for (int i = 0 ;i < stuList.getRecords().size() ; i++){
			if (stuList.getRecords().get(i).getTotalScore() != null && stuList.getRecords().get(i).getTotalScore() > maxScore){
				maxScore = stuList.getRecords().get(i).getTotalScore();
			}
		}
		statisticsVO.setMaxScore(maxScore);
		//最低分
		Double minScore = 100.0;
		for (int i = 0 ;i < stuList.getRecords().size() ; i++){
			if (stuList.getRecords().get(i).getTotalScore() != null && stuList.getRecords().get(i).getTotalScore() < minScore){
				minScore = stuList.getRecords().get(i).getTotalScore();
			}else if (minScore == 0.0){
				minScore = 0.0;
			}
		}
		statisticsVO.setMinScore(minScore);
		//将用户分数对象存入最终对象里面去
		statisticsVO.setStuSignUpComDO(stuList);
		statisticsVO.setSize(statisticsVO.getStuSignUpComDO().getSize());
		statisticsVO.setTotal(statisticsVO.getStuSignUpComDO().getTotal());
		return Result.ok(statisticsVO);
	}


	/**
	 * 个人统计总成绩导出
	 * */
	@ApiOperation(value = "赛场个人总统计", notes = "赛场个人总统计")
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@PostMapping("/personageTotalStatisticsList")
	@ResponseBody
	public Result<Page<StuSignUpComDO>> personageTotalStatisticsList(@RequestBody ExaminationsVo examinationsVo){
		Page<StuSignUpComDO> list = stuSignUpService.personageTotalStatisticsList(examinationsVo);


		return Result.ok(list);
	}


	/** 裁判打分处理  */
	private void  dateHandle (Page<StuSignUpComDO> stuList,List<MarkVO> secureMarkVOS,String competition) {
		for (int i = 0; i < stuList.getRecords().size(); i++) {
			ArrayList<MarkVO> MarkVO = new ArrayList<>();
			for (int k = 0; k < secureMarkVOS.size(); k++) {
				if (stuList.getRecords().get(i).getId().equals(secureMarkVOS.get(k).getSignId())) {//用户表学员表与打分表考试id匹配
					//计算裁判打的这个任务的总分
					String[] refereeScores = secureMarkVOS.get(k).getRefereeScore().split("/");//分割出来
					Double refereeScore = 0.0;
					for (String score : refereeScores) {
						refereeScore = Double.valueOf(score) + refereeScore;//两个人分数相加（计算）
					}
					secureMarkVOS.get(k).setRefereeScore(String.valueOf(refereeScore));//将裁判打的这个任务的总分存入这个对象中（这里就是这个裁判打的这个任务的总分）
					//计算出这个学员这个任务中所获得的总分（每个裁判总分除以裁判数）//这里k等于几就是有几个裁判
					//如果相同那么将这条数据添加到用户集合对象中去
					System.out.println("对象：" + secureMarkVOS.get(k));
					MarkVO.add(secureMarkVOS.get(k));
				}
			}
			if (MarkVO.size() > 0){
				stuList.getRecords().get(i).setMarkVO(MarkVO);
				stuList.getRecords().get(i).getMarkVO().size();//有多少条数就有多少个裁判
				Double TotalScore = 0.0;
				for (int j = 0; j < stuList.getRecords().get(i).getMarkVO().size(); j++) {//这个学员的所有裁判打的安全防护分
					TotalScore = Double.valueOf(stuList.getRecords().get(i).getMarkVO().get(j).getRefereeScore()) + TotalScore;//得到所有裁判打的总分（这里因为是个人总统计，所以这里是B赛场这个任务所得的总分）

				}
				//这里传入对应的参数，判断是那个赛场的得分
				if (competition.equals("安全防护")){//获得该学员安全防护的得分
					//最终学员这个任务所获得的总分（这里是B赛场安全防护得分），因为是要最终这个学员三个赛场的总得分所以要加上另外两个赛场的得分
					stuList.getRecords().get(i).setSecureScore(new BigDecimal(TotalScore / stuList.getRecords().get(i).getMarkVO().size()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());//最终得分为每个裁判打的总分除以裁判人数
				}else if(competition.equals("日常保健")){//获得该学员的日常保健的得分
					stuList.getRecords().get(i).setHealthScore(new BigDecimal(TotalScore / stuList.getRecords().get(i).getMarkVO().size()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
				}else if (competition.equals("生活照料")){
					stuList.getRecords().get(i).setLookAfterScore(new BigDecimal(TotalScore / stuList.getRecords().get(i).getMarkVO().size()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
				}else if (competition.equals("早期发展指导")){
					stuList.getRecords().get(i).setDevelopmentGuidanceScore(new BigDecimal(TotalScore / stuList.getRecords().get(i).getMarkVO().size()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
				}
			}

		}
	}



	/**
	 * 编辑分数回显（列表修改打分时是数据回显）
	 * competition ABC赛场
	 * ebatchId 考试批次
	 * cbatchId 认证批次
	 * @return
	 */
	@ResponseBody
	@GetMapping("/selEditData")
	public Result<?> selEditData(String competition,String cbatchId,String ebatchId,String signId,String competitionName){//(String competition,String ebatchId,String cbatchId,String stuId
		ArrayList<MarkDO> mrk = new ArrayList<>();
		ArrayList<Long> ids	=	new ArrayList<>();
		MrkRefereeVO mrkRefereeVO = new MrkRefereeVO();
		EntityWrapper<JzJinzhuanDispositionTopic> wrapperTopic = new EntityWrapper<>();
		wrapperTopic.eq("type",competition);//赛场（B）			//模板id
		List<JzJinzhuanDispositionTopic> topicList = dispositionTopicService.selectList(wrapperTopic);//B赛场所有模板任务
		for (int i = 0;i<topicList.size();i++){
			if (topicList.get(i).getTopicName().contains(competitionName)){//如果相等就是安全防护任务
				//模板id
				ids.add(topicList.get(i).getId());
			}
		}
		//先查询到这个用户有几个裁判打分
		Map map =new HashMap();
		map.put("competition",competition);
		map.put("signId",signId);
		if (cbatchId != null && cbatchId != ""){
			map.put("cbatchId",Long.valueOf(cbatchId));
		}
		if (ebatchId != null && ebatchId != ""){
			map.put("ebatchId",Long.valueOf(ebatchId));
		}
		map.put("jinzhuanTopicId",ids);
		List<MrkRefereeVO> mrkReferee = markService.selectReferee(map);
		//循环查询出裁判打的分数
		Map markMap =new HashMap();
		markMap.put("competition",competition);
		if (cbatchId != null && cbatchId != ""){
			markMap.put(" ",Long.valueOf(cbatchId));
		}
		markMap.put("signId",Long.valueOf(signId));
		for (int i = 0; i < mrkReferee.size(); i++){
			markMap.put("refereeId",mrkReferee.get(i).getRefereeId());
			//某个裁判的打分
			List<MarkVO> markVOS = markService.selectMark(markMap);
			//因为打的不是细则的分了，因此返回的数据中没有细则的具体的数据了这里就需要重新查询出对应的细则数据
			mrkReferee.get(i).setVideo(markVOS.get(0).getVideo());//视频
			mrkReferee.get(i).setPicture(markVOS.get(0).getPicture());//图片
			mrkReferee.get(i).setRefereeRemark(markVOS.get(0).getRefereeRemark());//裁判备注
			mrkReferee.get(i).setRefereeScore(String.valueOf(markVOS.get(0).getUpdateScore()));
			//查询出具体的细则描述等数据
			mrkReferee.get(i).setMarkVO(markVOS);
			mrkReferee.get(i).setMarkStatus(0);//0是已经打过分
		}
		/** 接口更改 */
		return Result.ok(mrkReferee);
	}









	/**
	 * 分数统计 最高分 最低分 总人数（列表最上面，最高分，最低分，赛场总人数）
	 * competition BC赛场
	 * ebatchId 考试批次
	 * cbatchId 认证批次
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getMinMaxStatistics")
	public Result<Map> getMinMaxStatistics(String competition,String ebatchId,String cbatchId){

		Map map =new HashMap();

		map.put("competition",competition); //根据赛场查询 abc必填

		if (StringUtil.notEmpty(ebatchId)) { //如果考试批次不为空
			map.put("ebatchId", ebatchId);
		}
		List<MarkDO> list = markService.selectListMarkDO(map);//查询出对应赛场的考生信息
		Map rstMap =new HashMap();

		if (list.size() >0 ){//如果数据大于0（这个赛场已经有人打过分了）
			Map selRefereeIdMap =new HashMap();
			selRefereeIdMap.put("ebatchId", ebatchId); //考试批次
			selRefereeIdMap.put("competition",competition); //abc赛场
			selRefereeIdMap.put("stuId",list.get(0).getSignId());//考生id
			List<Double> scoreSun = markService.selMaxMinScore(selRefereeIdMap); //查询出三个裁判十五次打分的总数
			scoreSun = scoreSun.stream().sorted().collect(Collectors.toList());
			rstMap.put("number",list.size()); //人数
			System.out.println("低0个数据："+scoreSun.get(0));
			//查询出对应的裁判信息（裁判配在了模板任务上面，那么通过模板任务id查询出对应的裁判，模板任务表里面有B或者C做判断）



			double min = (scoreSun.get(0)/3) * 0.7;//除以三就是三个裁判（裁判数据可能是不固定的，这里要变成活动的）
			min = new BigDecimal(min).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();//保留小数点后两位
			rstMap.put("min",min); //最低分
			double max = ((scoreSun.get(scoreSun.size()-1)/3) * 0.7);
			max = new BigDecimal(max).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();//保留小数点后两位
			rstMap.put("max",max);//最高分
		}else {
			rstMap.put("number",0); //人数
			rstMap.put("min",0); //最低分
			rstMap.put("max",0); //最高分
		}
		return Result.ok(rstMap);
	}



	private int selCompetitionTypeByEbatchId(String ebatchId){
		ExaminationBatchComDO examinationBatchComDO = examinationBatchComService.selectById(ebatchId);
		if (examinationBatchComDO == null){
			return 0;
		}
		return Integer.valueOf(examinationBatchComDO.getCompetitionType());
	}



	/**
	 * 裁判打分
	 * competition ABC赛场
	 * ebatchId 考试批次
	 * cbatchId 认证批次
	 * jinzhuanTopicId 金砖大赛实操题目id
	 * refereeId 裁判id
	 * signId 学生id
	 * video 视频
	 * picture 图片
	 * score 分数
	 * @return
	 */
	@ResponseBody
	@PostMapping("/judgeMark")
	public Result<Map> judgeMark(@RequestBody MarkVO  markVO){//String competition,String ebatchId,String cbatchId,String jinzhuanTopicId,String refereeId,String signId,String video,String picture,Double score,String raceId,String refereeRemark
		//实操打分
		if (markVO.getMarkDO() != null && markVO.getMarkDO().size() > 0){
	   List<MarkDO>  list = markVO.getMarkDO();
		for (int i = 0; i < list.size();i++){
		if (list.get(i).getCompetition().equals("null")){
			return Result.fail("赛场不能为空");
		}

		if (!StringUtil.notEmpty(list.get(i).getCompetition())){
			return Result.fail("赛场不能为空");
		}
		if (!StringUtil.notEmpty(String.valueOf(list.get(i).getEbatchId()))){
			return Result.fail("考试批次不能为空");
		}

		if (!StringUtil.notEmpty(String.valueOf(list.get(i).getCbatchId()))){
			return Result.fail("认证批次不能为空");
		}

		if (!StringUtil.notEmpty(String.valueOf(list.get(i).getRefereeId()))){
			return Result.fail("裁判不能为空");
		}

		if (!StringUtil.notEmpty(String.valueOf(list.get(i).getRaceId()))){
			return Result.fail("场次不能为空");
		}

		if (!StringUtil.notEmpty(String.valueOf(list.get(i).getJinzhuanTopicId()))){
			return Result.fail("题目不能为空");
		}

		if (!StringUtil.notEmpty(String.valueOf(list.get(i).getSignId()))){
			return Result.fail("考生不能为空");
		}

		if (String.valueOf(list.get(i).getEbatchId()).equals(String.valueOf(list.get(i).getCbatchId()))){
			return Result.fail("ebatchId 与 cbatchId不能相同！");
		}

		EntityWrapper wrapper =new EntityWrapper();
		wrapper.eq("competition",list.get(i).getCompetition() );//赛场
		wrapper.eq("race_id",list.get(i).getRaceId()); //赛场id
		wrapper.eq("ebatch_id",list.get(i).getEbatchId());//考试批次
		wrapper.eq("sign_id",list.get(i).getSignId()); //考生id
		wrapper.eq("referee_id",list.get(i).getRefereeId());//裁判id
		wrapper.eq("content_id",list.get(i).getContentId());//细则id
		wrapper.eq("jinzhuan_topic_id",list.get(i).getJinzhuanTopicId()); //题目id
		List marklist = markService.selectList(wrapper);
		if (marklist.size()>0){
			return Result.fail("请勿重复打分");
		}
		MarkDO markDO =new MarkDO();
		markDO.setCompetition(list.get(i).getCompetition()); //赛场
		markDO.setEbatchId(Long.valueOf(list.get(i).getEbatchId())); //考试批次id
		markDO.setRaceId(Long.valueOf(list.get(i).getRaceId())); //赛场id
		if (StringUtil.notEmpty(String.valueOf(list.get(i).getCbatchId()))){
			markDO.setCbatchId(Long.valueOf(list.get(i).getCbatchId())); //认证批次id
		}
		markDO.setJinzhuanTopicId(Long.valueOf(list.get(i).getJinzhuanTopicId())); //题目id
		markDO.setRefereeId(Long.valueOf(list.get(i).getRefereeId())); //裁判id
		markDO.setSignId(Long.valueOf(list.get(i).getSignId())); //考生id
		if (StringUtil.notEmpty(list.get(i).getVideo()) && !list.get(i).getVideo().equals("null")){
			markDO.setVideo(list.get(i).getVideo()); //视频
		}
		if (StringUtil.notEmpty(list.get(i).getPicture()) && !list.get(i).getPicture().equals("null")){
			markDO.setPicture(list.get(i).getPicture()); //图片
		}
		markDO.setScore(list.get(i).getScore()); //第一次打分分数
		markDO.setRefereeRemark(list.get(i).getRefereeRemark());
		markDO.setStatus(0); //实操打分状态为0
		markDO.setUpdateScore(list.get(i).getScore()); //最终分数修改后
		markDO.setCreateDate(new Date()); //创建时间
		markDO.setUpdateDate(new Date()); //修改时间
		markDO.setContentId(list.get(i).getContentId());
		markService.insert(markDO);
	}
   }else {
				if (markVO.getCompetition().equals("null")){
					return Result.fail("赛场不能为空");
				}

				if (!StringUtil.notEmpty(markVO.getCompetition())){
					return Result.fail("赛场不能为空");
				}
				if (!StringUtil.notEmpty(String.valueOf(markVO.getEbatchId()))){
					return Result.fail("考试批次不能为空");
				}

				if (!StringUtil.notEmpty(String.valueOf(markVO.getCbatchId()))){
					return Result.fail("认证批次不能为空");
				}

				if (!StringUtil.notEmpty(String.valueOf(markVO.getRefereeId()))){
					return Result.fail("裁判不能为空");
				}

				if (!StringUtil.notEmpty(String.valueOf(markVO.getRaceId()))){
					return Result.fail("场次不能为空");
				}

				if (!StringUtil.notEmpty(String.valueOf(markVO.getJinzhuanTopicId()))){
					return Result.fail("题目不能为空");
				}

				if (!StringUtil.notEmpty(String.valueOf(markVO.getSignId()))){
					return Result.fail("考生不能为空");
				}

				if (String.valueOf(markVO.getEbatchId()).equals(String.valueOf(markVO.getCbatchId()))){
					return Result.fail("ebatchId 与 cbatchId不能相同！");
				}

				EntityWrapper wrapper =new EntityWrapper();
				wrapper.eq("race_id",markVO.getRaceId()); //赛场
				wrapper.eq("ebatch_id",markVO.getEbatchId());//考试批次
				wrapper.eq("sign_id",markVO.getSignId()); //考生id
				wrapper.eq("referee_id",markVO.getRefereeId());//裁判id
				List marklist = markService.selectList(wrapper);
				if (marklist.size()>0){
					return Result.fail("请勿重复打分");
				}
				MarkDO markDO =new MarkDO();
				markDO.setCompetition(markVO.getCompetition()); //赛场
				markDO.setEbatchId(Long.valueOf(markVO.getEbatchId())); //考试批次id
				markDO.setRaceId(Long.valueOf(markVO.getRaceId())); //赛场id
				if (StringUtil.notEmpty(String.valueOf(markVO.getCbatchId()))){
					markDO.setCbatchId(Long.valueOf(markVO.getCbatchId())); //认证批次id
				}
				markDO.setRefereeId(Long.valueOf(markVO.getRefereeId())); //裁判id
				markDO.setSignId(Long.valueOf(markVO.getSignId())); //考生id
				if (StringUtil.notEmpty(markVO.getVideo()) && !markVO.getVideo().equals("null")){
					markDO.setVideo(markVO.getVideo()); //视频
				}
				if (StringUtil.notEmpty(markVO.getPicture()) && !markVO.getPicture().equals("null")){
					markDO.setPicture(markVO.getPicture()); //图片
				}
				markDO.setScore(Double.valueOf(markVO.getUpdateScore())); //第一次打分分数
				markDO.setRefereeRemark(markVO.getRefereeRemark());
				markDO.setStatus(0); //实操打分状态为0
				markDO.setUpdateScore(Double.valueOf(markVO.getUpdateScore())); //最终分数修改后
				markDO.setCreateDate(new Date()); //创建时间
				markDO.setUpdateDate(new Date()); //修改时间
				markService.insert(markDO);
		}
		return Result.ok();
	}


	/**
	 * 查询考生备注（对应裁判给考生的备注）
	 * */
	@ResponseBody
	@PostMapping("/examineeRemark")
	public  Result examineeRemark(Long refereeId,Long ebatchId,Long signId,Long  raceId){
			MarkDO markDO = new MarkDO();
			markDO.setRaceId(raceId);//赛场id
			markDO.setEbatchId(ebatchId);//考试批次
			markDO.setSignId(signId);//用户id
			markDO.setRefereeId(refereeId);//裁判id
			MarkDO markDO1 = markService.selectOne(markDO);
			return Result.ok(markDO1);
	}



	/**
	 * 管理员改分
	 */
	@ResponseBody
	@PostMapping("/conservatorJudgeMark")
	public Result<Object> conservatorJudgeMark(@RequestBody MarkVO  markVO){//String id,Double score,String video,String picture,String  refereeRemark

		if (markVO.getMarkDO() != null && markVO.getMarkDO().size() > 0) {
			List<MarkDO> list = markVO.getMarkDO();
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getUpdateScore() != null && !list.get(i).getUpdateScore().equals("")) {
					System.out.println("分数：" + list.get(i).getUpdateScore());
					EntityWrapper wrapper = new EntityWrapper();
					wrapper.eq("sign_id", list.get(i).getSignId());
					wrapper.eq("referee_id", list.get(i).getRefereeId());
					wrapper.eq("race_id", list.get(i).getRaceId());
					wrapper.eq("cbatch_id",list.get(i).getCbatchId());
					if (Objects.nonNull(list.get(i).getJinzhuanTopicId())){
						wrapper.eq("jinzhuan_topic_id",list.get(i).getJinzhuanTopicId());
					}
					if (Objects.nonNull(list.get(i).getEbatchId())){
						wrapper.eq("ebatch_id",list.get(i).getEbatchId());
					}
//					wrapper.eq("ebatch_id",list.get(i).getEbatchId());
//					wrapper.eq("content_id", list.get(i).getContentId());
					MarkDO markDO = markService.selectOne(wrapper);
					markDO.setUpdateScore(list.get(i).getUpdateScore());
					markDO.setRefereeRemark(list.get(i).getRefereeRemark());
					if (StringUtil.notEmpty(list.get(i).getVideo()) && !list.get(i).getVideo().equals("null")) {//视频
						markDO.setVideo(list.get(i).getVideo());
					}
					if (StringUtil.notEmpty(list.get(i).getPicture()) && !list.get(i).getPicture().equals("null")) {//图片
						markDO.setPicture(list.get(i).getPicture());
					}
					markService.updateById(markDO);
				}
			}
		}else {
			MarkDO markDO = new MarkDO();
			markDO.setId(markVO.getId());
			markDO.setUpdateScore(Double.valueOf(markVO.getUpdateScore()));
			if (StringUtil.notEmpty(markVO.getVideo()) && !markVO.getVideo().equals("null")) {//视频
				markDO.setVideo(markVO.getVideo());
			}
			if (StringUtil.notEmpty(markVO.getPicture()) && !markVO.getPicture().equals("null")) {//图片
				markDO.setPicture(markVO.getPicture());
			}
			markDO.setRefereeRemark(markVO.getRefereeRemark());
			markService.updateById(markDO);
		}
		return Result.ok();
	}



	/**
	 * 管理员改分
	 */
	@ResponseBody
	@PostMapping("/conservatorJudgeMarks")
	public Result<Object> conservatorJudgeMarks(@RequestParam(value="ids[]")List<String> ids,@RequestParam(value="scores[]")List<Double> scores){

		List<MarkDO> list = markService.selectBatchIds(ids);

		for (int i=0;i<list.size();i++){
			list.get(i).setUpdateScore(scores.get(i));
		}
		markService.updateBatchById(list);
		return Result.ok();
	}


	/**
	 * 理论成绩统计
	 * */
	@ApiOperation(value = "理论成绩统计", notes = "理论成绩统计")
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@PostMapping("/personageTotalAList")
	@ResponseBody
	public Result<Page<StuSignUpComDO>> personageTotalAList(@RequestBody ExaminationsVo examinationsVo){
		Page<StuSignUpComDO> list = stuSignUpService.personageTotalAList(examinationsVo);


		return Result.ok(list);
	}


	/** 理论成绩统计导出 */
	@ResponseBody
	@GetMapping("/personageTotalAImport")
	public Result<String>  personageTotalAImport(Long cbatchId,HttpServletResponse response){
		List<MarkAExcel> excelList = stuSignUpService.personageTotalAImport(cbatchId, response);
		EasyPoiUtil.exportExcel(excelList, "理论成绩统计导出",  "理论成绩统计导出", MarkAExcel.class, "理论成绩统计导出", response);
		return Result.ok("导出成功！");
	}



	/**
	 * 加密列表
	 * */
	@ApiOperation(value = "加密列表", notes = "加密列表")
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@PostMapping("/personageTotalStatisticsLists")
	@ResponseBody
	public Result<Page<StuSignUpComDO>> personageTotalStatisticsLists(@RequestBody ExaminationsVo examinationsVo){
		Page<StuSignUpComDO> list = stuSignUpService.personageTotalStatisticsLists(examinationsVo);

		return Result.ok(list);
	}

	/** 解密导出 */
	@ResponseBody
	@GetMapping("/personageStatisticsexcelss")
	public Result<String>  personageStatisticsexcelss(Long cbatchId,HttpServletResponse response){
		List<JiemiStatisticsExcel> excelList = stuSignUpService.personageStatisticsexcelss(cbatchId, response);
		EasyPoiUtil.exportExcel(excelList, "选手解密信息导出",  "选手解密信息导出", JiemiStatisticsExcel.class, "选手解密信息导出", response);
		return Result.ok("导出成功！");
	}



	/** 考生文件导出 */
	@ApiOperation(value = "压缩包下载", notes = "压缩包下载")
	@ApiImplicitParams({
	})
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@ResponseBody
	@PostMapping(value = "/stuFileDownload")
	public  Result  stuFileDownload(@RequestParam Long cbatchId,HttpServletResponse response){
		return markService.stuFileDownload(cbatchId,response);
	}

	/**
	 * A赛场录分页面
	 * @return
	 */
	@GetMapping("/recordA")
	String markRecordA(){
		return "jzweb/record/recordPointsA";
	}

	/**
	 * A赛场录分保存页面入口
	 * @return
	 */
	@GetMapping("/recordA/add")
	String markRecordASave(){
		return "/jzweb/record/addA";
	}

	/**
	 * A赛场录分保存页面入口 2级
	 * @return
	 */
	@GetMapping("/recordA/add2")
	String markRecordASave2(){
		return "/jzweb/record/addA2";
	}

	/**
	 * A赛场录分编辑页面入口
	 * @return
	 */
	@GetMapping("/recordA/edit")
	String markRecordAEdit(){
		return "/jzweb/record/editA";
	}

	/**
	 * A赛场录分编辑页面入口 2级别
	 * @return
	 */
	@GetMapping("/recordA/edit2")
	String markRecordAEdit2(){
		return "/jzweb/record/editA2";
	}


	/**
	 * 录分保存
	 */
	@ApiOperation(value = "A赛场录分列表", notes = "A赛场录分列表")
	@ApiImplicitParams({
	})
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@ResponseBody
	@PostMapping(value = "/recordASave")
	public  Result  recordASave(@RequestBody List<MarkDO> markDOS){
		Result result = markService.recordASave(markDOS);
		return result;
	}


}
